package com.aegis.gateway.config;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube
 */
@Configuration
public class GatewayConfig {

//    @Bean(name = "tokenKeyResolver")
//    public KeyResolver tokenKeyResolver() {
//        return exchange -> {
//            if (!StpUtil.isLogin()) {
//                return Mono.just("no-token");
//            }
//            return Mono.just(StpUtil.getLoginId().toString());
//        };
//    }


//    @Bean(name = "ipKeyResolver")
//    public KeyResolver ipKeyResolver() {
//        return exchange -> {
//            // 从请求的 "X-Forwarded-For" 头部获取 IP 地址
//            String ip = exchange.getRequest().getHeaders().getFirst("X-Forwarded-For");
//            if (ip == null) {
//                ip = Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getHostString(); // fallback to remote address
//            }
//            return Mono.just(ip);
//        };
//    }

}
